package com.ruoyi.wb.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wb.domain.WbEngineerEnter;
import com.ruoyi.wb.enmus.WbAuditType;
import com.ruoyi.wb.mapper.WbEngineerEnterMapper;
import com.ruoyi.wb.service.IWbEngineerEnterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 工程师入驻
 * Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-02
 */
@Service
public class WbEngineerEnterServiceImpl implements IWbEngineerEnterService {
    @Autowired
    private WbEngineerEnterMapper wbEngineerEnterMapper;

    /**
     * 查询工程师入驻
     *
     * @param id 工程师入驻
     *           主键
     * @return 工程师入驻
     */
    @Override
    public WbEngineerEnter selectWbEngineerEnterById(Long id) {
        return wbEngineerEnterMapper.selectWbEngineerEnterById(id);
    }

    /**
     * 查询工程师入驻
     * 列表
     *
     * @param wbEngineerEnter 工程师入驻
     * @return 工程师入驻
     */
    @Override
    public List<WbEngineerEnter> selectWbEngineerEnterList(WbEngineerEnter wbEngineerEnter) {
        return wbEngineerEnterMapper.selectWbEngineerEnterList(wbEngineerEnter);
    }

    /**
     * 新增工程师入驻
     *
     * @param wbEngineerEnter 工程师入驻
     * @return 结果
     */
    @Override
    public int insertWbEngineerEnter(WbEngineerEnter wbEngineerEnter) {
        //查询是否已经新增过
        SysUser user = SecurityUtils.getLoginUser().getUser();
        WbEngineerEnter item = new WbEngineerEnter();
        item.setCreateBy(user.getUserId().toString());
        List<WbEngineerEnter> wbEngineerEnters = wbEngineerEnterMapper.selectWbEngineerEnterList(item);
        long count = wbEngineerEnters.stream().filter(t -> !t.getAuditType().equals(3L)).count();
        if (count > 0) {
            throw new RuntimeException("您已经申请过入驻无法再次申请");
        }


        wbEngineerEnter.setName(user.getUserName());
        wbEngineerEnter.setPhone(user.getPhonenumber());
        wbEngineerEnter.setSex(Long.parseLong(user.getSex()));
        wbEngineerEnter.setAuditType(1L);
        wbEngineerEnter.setCreateBy(user.getUserId().toString());
        wbEngineerEnter.setCreateTime(DateUtils.getNowDate());
        return wbEngineerEnterMapper.insertWbEngineerEnter(wbEngineerEnter);
    }

    /**
     * 修改工程师入驻
     *
     * @param wbEngineerEnter 工程师入驻
     * @return 结果
     */
    @Override
    public int updateWbEngineerEnter(WbEngineerEnter wbEngineerEnter) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (ObjectUtils.isEmpty(wbEngineerEnter.getId())) {
            throw new RuntimeException("id必填");
        }
        WbEngineerEnter wbEngineerEnter1 = wbEngineerEnterMapper.selectWbEngineerEnterById(wbEngineerEnter.getId());
        if (!ObjectUtils.isEmpty(wbEngineerEnter.getAuditType()) &&
                wbEngineerEnter.getAuditType().equals(WbAuditType.BEENTERING.getCode()) &&
                !wbEngineerEnter1.getAuditType().equals(WbAuditType.BEENTERING.getCode())) {
            wbEngineerEnter.setCauseOfRejection("您的工程师入驻申请已经通过,请保持手机畅通。");
            wbEngineerEnter.setIfRead(1L);
        }


        if ((!ObjectUtils.isEmpty(wbEngineerEnter.getAuditType()) &&
                wbEngineerEnter.getAuditType().equals(WbAuditType.BEENTERING.getCode()) &&
                !wbEngineerEnter1.getAuditType().equals(WbAuditType.BEENTERING.getCode()))
                || (!ObjectUtils.isEmpty(wbEngineerEnter.getAuditType()) &&
                wbEngineerEnter.getAuditType().equals(WbAuditType.NOTSETTLED.getCode()))
        ) {
            wbEngineerEnter.setUpdateBy(user.getNickName());
            wbEngineerEnter.setUpdateTime(DateUtils.getNowDate());
        }


        return wbEngineerEnterMapper.updateWbEngineerEnter(wbEngineerEnter);
    }

    /**
     * 批量删除工程师入驻
     *
     * @param ids 需要删除的工程师入驻主键
     * @return 结果
     */
    @Override
    public int deleteWbEngineerEnterByIds(Long[] ids) {
        return wbEngineerEnterMapper.deleteWbEngineerEnterByIds(ids);
    }

    /**
     * 删除工程师入驻信息
     *
     * @param id 工程师入驻主键
     * @return 结果
     */
    @Override
    public int deleteWbEngineerEnterById(Long id) {
        return wbEngineerEnterMapper.deleteWbEngineerEnterById(id);
    }

    @Override
    public WbEngineerEnter byUser() {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        return wbEngineerEnterMapper.byUser(userId);
    }
}
